<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of solution
 *
 * @author srkrishnan
 */

/**

 * @property CI_Loader $load

 * @property CI_Form_validation $form_validation

 * @property CI_Input $input

 * @property CI_Email $email

 * @property CI_DB_active_record $db

 * @property CI_DB_forge $dbforge
 * 
 * @property CI_DB_driver $result

 */
class Solution extends CI_Model {

    function Solution() {
        parent::__construct();
        $this->load->database();
        $this->load->library('session');
    }

    function insertsolution($code, $languageid, $logic) {

        $voteup = 0;
        $votedown = 0;

        $query = 'insert into solution 
	(solutionid, 
	logic, 
	solution, 
	codelangid, 
	voteup, 
	votedown
	) 
        values
        (0' .
                ',"' . mysql_escape_string($logic) . '",' .
                '"' . mysql_escape_string($code) . '",' .
                '\'' . $languageid . '\',' .
                '\'' . $voteup . '\',' .
                '\'' . $votedown . '\''
                . ')';


        try {
            $res = $this->db->query($query);

            $solutionid = $this->db->insert_id();

            $userid = $this->session->userdata('userid');
            $problemid = $this->session->userdata('problemid');

            $now = date("Y-m-d H:i:s");

            $usersolutionquery = 'insert into usersolutions 
	(usersolutionid, 
	userid, 
	problemid, 
	solutionid, 
	addedtime
	)
	values
	(0' .
                    ',' . $userid .
                    ',' . $problemid .
                    ',' . $solutionid .
                    ',\'' . $now . '\''
                    . ')';
            $res = $this->db->query($usersolutionquery);
        } catch (Exception $e) {
            $E = $e;
        }
        $r = $res;
    }

    function getsolution($userid, $problemid, &$data) {

        $data['solution'] = '';
        $data['logic'] = '';
        $data['langid']='';

        $selectsolutionid = 'select 	usersolutionid, 
	userid, 
	problemid, 
	solutionid, 
	addedtime
	 
	from 
	usersolutions 
        where 
        userid=' . $userid . ' and problemid=' . $problemid
                . ' order by addedtime desc
	
	limit 1;';

        $result = $this->db->query($selectsolutionid);
        if ($result->num_rows() == 0) {
            //solution does not exists
        } else {
            $solutionidresult = $result->result();
            $solutionid = $solutionidresult[0]->solutionid;

            $selectsolution = 'select 	solutionid, 
	logic, 
	solution, 
	codelangid, 
	voteup, 
	votedown
	 
	from 
	solution 
	where
	solutionid=' . $solutionid .
                    ' limit 1';
            $solutionresult = $this->db->query($selectsolution);

            if ($solutionresult->num_rows() == 0) {
                //solution doesnot exist
            } else {
                $solutionlogicresult = $solutionresult->result();
                $data['solution'] = $solutionlogicresult[0]->solution;
                $data['logic'] = $solutionlogicresult[0]->logic;
                $data['langid']= $solutionlogicresult[0]->codelangid;
            }
        }
        $r = $result;
        ;
    }

}

?>
